package cn.yeziji.forum.utils.entity;

import cn.yeziji.forum.common.AliCloudOss;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;
import java.io.InputStream;

/**
 * 阿里云 oss 配置实体类
 *
 * @author gzkemays
 * @since 2021/12/21 15:54
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AliOssConfig {
  private String endPoint;
  private String accessKey;
  private String secretKey;
  private String bucketName;
  private String fileName;
  private String savePath;
  private String ext;
  private String returnUrl;
  private String localPath;
  private InputStream stream;

  public static AliOssConfigBuilder builder() {
    return new AliOssConfigBuilder();
  }

  public String getOssFilePath() {
    // 避免双斜杠
    return (this.savePath + "/" + this.fileName).replace("//", "/");
  }

  public String getLocalFilePath() {
    return (this.localPath + File.separator + this.fileName).replace("//", "/");
  }

  public static AliOssConfigBuilder cloudOss(AliCloudOss oss) {
    return AliOssConfig.builder()
        .accessKey(oss.getAccessKey())
        .secretKey(oss.getSecurityKey())
        .endPoint(oss.getEndPoint())
        .savePath(oss.getSavePath())
        .bucketName(oss.getBucket())
        .returnUrl(oss.getReturnUrl());
  }

  public static AliOssConfigBuilder cloudPayOss(AliCloudOss oss) {
    return AliOssConfig.builder()
        .accessKey(oss.getAccessKey())
        .secretKey(oss.getSecurityKey())
        .endPoint(oss.getPayEndPoint())
        .bucketName(oss.getPayBucket())
        .returnUrl(oss.getPayReturnUrl());
  }

  public static final class AliOssConfigBuilder {
    private String endPoint;
    private String accessKey;
    private String secretKey;
    private String bucketName;
    private String fileName;
    private String savePath;
    private String ext;
    private String localPath;
    private String returnUrl;
    private InputStream stream;

    private AliOssConfigBuilder() {}

    public AliOssConfigBuilder endPoint(String endPoint) {
      this.endPoint = endPoint;
      return this;
    }

    public AliOssConfigBuilder accessKey(String accessKey) {
      this.accessKey = accessKey;
      return this;
    }

    public AliOssConfigBuilder secretKey(String secretKey) {
      this.secretKey = secretKey;
      return this;
    }

    public AliOssConfigBuilder bucketName(String bucketName) {
      this.bucketName = bucketName;
      return this;
    }

    public AliOssConfigBuilder fileName(String fileName) {
      this.fileName = fileName;
      return this;
    }

    public AliOssConfigBuilder savePath(String savePath) {
      this.savePath = savePath;
      return this;
    }

    public AliOssConfigBuilder ext(String ext) {
      this.ext = ext;
      return this;
    }

    public AliOssConfigBuilder returnUrl(String returnUrl) {
      this.returnUrl = returnUrl;
      return this;
    }

    public AliOssConfigBuilder stream(InputStream stream) {
      this.stream = stream;
      return this;
    }

    public AliOssConfigBuilder tempSavePath() {
      this.savePath = "temp";
      return this;
    }

    public AliOssConfigBuilder localPath(String localPath) {
      this.localPath = localPath;
      return this;
    }

    public AliOssConfig build() {
      AliOssConfig aliOssConfig = new AliOssConfig();
      aliOssConfig.setEndPoint(endPoint);
      aliOssConfig.setAccessKey(accessKey);
      aliOssConfig.setSecretKey(secretKey);
      aliOssConfig.setBucketName(bucketName);
      aliOssConfig.setFileName(fileName);
      aliOssConfig.setSavePath(savePath);
      aliOssConfig.setExt(ext);
      aliOssConfig.setReturnUrl(returnUrl);
      aliOssConfig.setStream(stream);
      aliOssConfig.setLocalPath(localPath);
      return aliOssConfig;
    }
  }
}
